package com.ht.dsp;

import _61970.core.Equipment;
import _61970.topology.TopologicalNode;
import _61970.wires.*;
import com.ht.aems.ieeeformat.BranchData;
import com.ht.aems.ntp.CimToIEEE;
import devmodel.PhysDevToIeeeIndex;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: Dong Shufeng
 * Date: 11-8-25
 */
public class CalModelUtil {
    public static PhysDevToIeeeIndex createIndex(CimToIEEE bridge) {
        PhysDevToIeeeIndex index = new PhysDevToIeeeIndex();
        Map<String, Integer> map = new HashMap<String, Integer>(bridge.getWinding2branch().size());
        for (TransformerWinding w : bridge.getWinding2branch().keySet())
            map.put(w.getMRID(), bridge.getWinding2branch().get(w).getId());
        index.setWinding2branch(map);

        map = new HashMap<String, Integer>(bridge.getAcline2branch().size());
        for (ACLineSegment line : bridge.getAcline2branch().keySet())
            map.put(line.getMRID(), bridge.getAcline2branch().get(line).getId());
        index.setAcline2branch(map);

        HashMap<Integer, String> map2 = new HashMap<Integer, String>(bridge.getBranch2acline().size());
        for (BranchData branch : bridge.getBranch2acline().keySet())
            map2.put(branch.getId(), bridge.getBranch2acline().get(branch).getMRID());
        index.setBranch2acline(map2);

        Map<Integer, List<String>> generators = new HashMap<Integer, List<String>>(bridge.getCimTopoIsland().getGeneratorNum());
        Map<Integer, List<String>> loads = new HashMap<Integer, List<String>>(bridge.getCimTopoIsland().getLoadNum());
        Map<Integer, List<String>> busbars = new HashMap<Integer, List<String>>(bridge.getCimTopoIsland().getBusbarNum());
        Map<Integer, List<String>> shuntCompensators = new HashMap<Integer, List<String>>(bridge.getCimTopoIsland().getShuntCompensatorNum());
        index.setGenerators(generators);
        index.setLoads(loads);
        index.setBusbars(busbars);
        index.setShuntCompensators(shuntCompensators);
        for (TopologicalNode node : bridge.getNode2bus().keySet()) {
            List<SynchronousMachine> gens = bridge.getCimTopoIsland().getGenerators().get(node);
            if (gens != null && gens.size() > 0) {
                ArrayList<String> value = new ArrayList<String>(gens.size());
                generators.put(bridge.getNode2bus().get(node).getBusNumber(), value);
                for (Equipment m : gens)
                    value.add(m.getMRID());
            }
            List<EnergyConsumer> load = bridge.getCimTopoIsland().getLoads().get(node);
            if (load != null && load.size() > 0) {
                ArrayList<String> value = new ArrayList<String>(load.size());
                loads.put(bridge.getNode2bus().get(node).getBusNumber(), value);
                for (Equipment m : load)
                    value.add(m.getMRID());
            }
            List<BusbarSection> buses = bridge.getCimTopoIsland().getBusbars().get(node);
            if (buses != null && buses.size() > 0) {
                ArrayList<String> value = new ArrayList<String>(buses.size());
                busbars.put(bridge.getNode2bus().get(node).getBusNumber(), value);
                for (Equipment m : buses)
                    value.add(m.getMRID());
            }
            List<ShuntCompensator> compensators = bridge.getCimTopoIsland().getShuntCompensators().get(node);
            if (compensators != null && compensators.size() > 0) {
                ArrayList<String> value = new ArrayList<String>(compensators.size());
                shuntCompensators.put(bridge.getNode2bus().get(node).getBusNumber(), value);
                for (Equipment m : compensators)
                    value.add(m.getMRID());
            }
        }
        return index;
    }
}
